<?php

namespace app\wp\controller;
use think\Db;
use app\wp\model\MovieInfo;
class Movie extends Common{
    public function index(){
        
    }
   
    public function lists(){
        $keyword = input("keyword");
        
        $list = db("movie")->where("title" , 'LIKE' , '%'. $keyword . '%')
                ->whereOr('starring' , 'LIKE' ,  '%'. $keyword . '%')
                ->order("id desc")->paginate(15 , false , [
            'type'      => 'Bootstrap',
            'var_page'  => 'page',            
        ]);
        $this->assign("list" , $list);
        return $this->fetch();
    }
    
    public function add(){
        return $this->fetch();
    }
    
    public function import(){
        $this->assign("data" , null);
        return $this->fetch();
    }
    
    public function save(){
        $id = input("id" , 0 , 'intval');
        $data = [
            'title'         => input("title" , '' , 'htmlspecialchars'), 
            'summary'       => input("summary" , '' , 'htmlspecialchars'),
            'genres'        => input("genres"  , '' , 'htmlspecialchars'),
            'year'          => input("year" , 0 , 'intval'),
            'country'       => input("countries" , '' , 'htmlspecialchars'),
            'image'        => input("image" , '' , 'htmlspecialchars'),
            'director'      => input("director" , '' , ''),
            'starring'      => input("starring" , '' , ''),
            'runningtime'   => input("runningtime"  , '' , 'intval'),
            'created'       => time()
        ];
        $movie = $_POST["movie"];
        $haobao = $_POST["haobao"];
        
       if($id){
           //删除海报
           db("video")->where(['movieid' => $id])->delete();
           //删除视频
           db("stills")->where(['movieid' => $id])->delete();
           db("movie")->where(['id' => $id])->update($data);
           
           foreach ($movie as $key => $value){
               if($value){
                   $movie_data[] = [
                        'movieid'        => $id,
                        'url'            => $value
                    ];
               }
           }
           if(!empty($movie_data)){
               db("video")->insertAll($movie_data);
           }
           foreach($haobao as $key =>$value){
               if($value){
                   $haobao_data[] = [
                       'movieid' => $id,
                       'imgurl'  => $value
                   ];
               }
           }           
           if(!empty($haobao_data)){
               db("stills")->insertAll($haobao_data);
           }
           
           return [
                'status' => 0,
                'msg'    => '更新成功'
            ];
       }else{
           $newid = db("movie")->insertGetId($data);
           
           foreach ($movie as $key => $value){
               if($value){
                   $movie_data[] = [
                        'movieid'        => $newid,
                        'url'            => $value
                    ];
               }
           }
           if(!empty($movie_data)){
               db("video")->insertAll($movie_data);
           }           
           
           foreach($haobao as $key =>$value){
               if($value){
                   $haobao_data[] = [
                       'movieid' => $newid,
                       'imgurl'  => $value
                   ];
               }
           }
           
           if(!empty($haobao_data)){
               db("stills")->insertAll($haobao_data);
           }
           
           if($newid){
               return [
                   'status' => 1,
                   'msg'    => '成功添加电影'
               ];
           }else{
               return [
                   'status' => 0,
                   'msg'    => '添加电影失败'
               ];
           }
       }
    }
    
    public function douban_query(){
        
        $keyword = input("keyword");
        $api_url = "http://api.douban.com/v2/movie/search?count=4&q={$keyword}";
        $json = https_request($api_url);
        $arr = json_decode($json , true);
        $subjects = $arr['subjects'];
        return $subjects;
    }
    
    public function getMovieInfo(){
        $id =input("id");
        $api_url = "http://api.douban.com/v2/movie/subject/{$id}";
        $json = https_request($api_url);
        $arr = json_decode($json , true);
       
        return $arr;
    }
    
    public function getMovieModel(){
        $id = input("fid" , '0' , 'intval' );
        
        return MovieInfo::info($id);
    }
}

